package winOrDie.graphicsInterface.GameRoom;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;

import winOrDie.Application.WoD_Application_Data;
import winOrDie.graphicsInterface.WoD_Composite_Type;
import winOrDie.graphicsInterface.GameMenu.Composite_Configuration_Event;

public class Composite_GameRoom extends Composite implements Composite_Options_Listener, Composite_Rols_Listener, Composite_Maps_Listener{

	
	private Label label_CaptainTeam = null;
	private Label label_PiratesTeam = null;
	
	private Composite_Peer_List_NEW playerList_CaptainTeam = null;
	private Composite_Peer_List_NEW playerList_PiratesTeam = null;
	private Composite_Peer_List peerList = null;
	private Composite_Peer_List_NEW peerList_NEW = null;
	private Composite_Chat compositeChat = null;
	private Composite_Options compositeOptions = null;
	private Composite_Rols compositeRols = null;	
	private Composite_Maps_List compositeMaps = null;
	
	
	public Composite_Peer_List_NEW getPlayerList_CaptainTeam() {
		return playerList_CaptainTeam;
	}


	public Composite_Peer_List_NEW getPlayerList_PiratesTeam() {
		return playerList_PiratesTeam;
	}


	public Composite_Peer_List getPeerList() {
		return peerList;
	}


	public Composite_Peer_List_NEW getPeerList_NEW() {
		return peerList_NEW;
	}

	public Composite_GameRoom(Composite parent, int style) {
		super(parent, style);
		initialize();
	}

	private void initialize() {
		setSize(new Point(300, 300));
		
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		
		setLayout(gridLayout);
		
		create_Label_CaptainTeam();
		Label label = new Label(this, SWT.NONE);
		create_Label_PiratesTeam();
		
		create_List_CaptainTeam();
//		create_List_Peers();
		create_List_Perrs_NEW();
		create_List_PiratesTeam();
		get_Composite_Chat();
		get_Composite_Options();
		get_Composite_Rols().setVisible(false);
		get_Composite_Options().get_Button_Start_Game().setEnabled(false);
		create_List_Maps().setVisible(false);		
	}

	
	private void create_Label_CaptainTeam()
	{
		GridData gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = SWT.CENTER;
		gridData.verticalAlignment = SWT.CENTER;
		
		label_CaptainTeam = new Label(this, SWT.NONE);
		label_CaptainTeam.setLayoutData(gridData);
		
		label_CaptainTeam.setText("Buenos");
		
		
	}
	
	private void create_Label_PiratesTeam()
	{
		GridData gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = SWT.CENTER;
		gridData.verticalAlignment = SWT.CENTER;
		
		label_PiratesTeam = new Label(this, SWT.NONE);
		label_PiratesTeam.setLayoutData(gridData);
		
		label_PiratesTeam.setText("Piratas");

		
	}
	
	
	private void create_List_CaptainTeam()
	{
		GridData gridData = new GridData();
		gridData.horizontalAlignment = SWT.FILL;
		gridData.verticalAlignment = SWT.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.minimumHeight = 100;
		gridData.minimumWidth = 100;
		
		
		playerList_CaptainTeam = new Composite_Peer_List_NEW(this, SWT.NONE, WoD_Composite_Type.TEAM_COMPOSITE);
		playerList_CaptainTeam.setLayoutData(gridData);
		
	}
	
	private void create_List_PiratesTeam()
	{
		GridData gridData = new GridData();
		gridData.horizontalAlignment = SWT.FILL;
		gridData.verticalAlignment = SWT.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.minimumHeight = 100;
		gridData.minimumWidth = 100;
		
		playerList_PiratesTeam = new Composite_Peer_List_NEW(this, SWT.NONE, WoD_Composite_Type.TEAM_COMPOSITE);
		playerList_PiratesTeam.setLayoutData(gridData);
		
	}

	
	private void create_List_Perrs_NEW()
	{
		GridData gridData = new GridData();
		gridData.horizontalAlignment = SWT.CENTER;
		gridData.verticalAlignment = SWT.CENTER;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.minimumHeight = 100;
		gridData.minimumWidth = 100;
		
		peerList_NEW = new Composite_Peer_List_NEW(this, SWT.NONE, WoD_Composite_Type.PEER_COMPOSITE);
		peerList_NEW.setLayoutData(gridData);
	}
	
	public Composite_Maps_List create_List_Maps()
	{
		if (compositeMaps == null) {
			GridData gridData = new GridData();
			gridData.horizontalSpan = SWT.LEFT;
			gridData.horizontalAlignment = SWT.LEFT;
			gridData.verticalAlignment = SWT.BOTTOM;
			gridData.grabExcessHorizontalSpace = true;
			gridData.grabExcessVerticalSpace = true;
			gridData.minimumHeight = 50;
			gridData.minimumWidth = 200;

			compositeMaps = new Composite_Maps_List(this, SWT.NONE, WoD_Composite_Type.MAPS_COMPOSITE);
			compositeMaps.setLayoutData(gridData);
			
			compositeMaps.add_Composite_Maps_Listener(this);
		}
		return compositeMaps;
	}
	
	private void create_List_Peers()
	{
		GridData gridData = new GridData();
		gridData.horizontalAlignment = SWT.CENTER;
		gridData.verticalAlignment = SWT.CENTER;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.minimumHeight = 100;
		gridData.minimumWidth = 100;
		
		peerList = new Composite_Peer_List(this, SWT.NONE);
		peerList.setLayoutData(gridData);
		
		
	}
	
	
	public Composite_Chat get_Composite_Chat()
	{
		if (compositeChat == null) {
			GridData gridData = new GridData();
			gridData.horizontalSpan = 2;
			gridData.horizontalAlignment = SWT.FILL;
			gridData.verticalAlignment = SWT.FILL;
			gridData.grabExcessHorizontalSpace = true;
			gridData.grabExcessVerticalSpace = true;
			compositeChat = new Composite_Chat(this, SWT.NONE);
			compositeChat.setLayoutData(gridData);
		}
		return compositeChat;
		
	}
	
	
	public Composite_Options get_Composite_Options()
	{
		if (compositeOptions == null) {
			GridData gridData = new GridData();
			gridData.horizontalSpan = 1;
			gridData.horizontalAlignment = SWT.CENTER;
			gridData.verticalAlignment = SWT.CENTER;
			gridData.grabExcessHorizontalSpace = true;
			gridData.grabExcessVerticalSpace = true;
			compositeOptions = new Composite_Options(this, SWT.NONE);
			compositeOptions.setLayoutData(gridData);
			
			compositeOptions.add_Composite_Options_Listener(this);
		}
		return compositeOptions;
	}
	
	public Composite_Rols get_Composite_Rols()
	{
		if (compositeRols == null) {
			GridData gridData = new GridData();
			gridData.horizontalSpan = 1;
			gridData.horizontalAlignment = SWT.CENTER;
			gridData.verticalAlignment = SWT.CENTER;
			gridData.grabExcessHorizontalSpace = true;
			gridData.grabExcessVerticalSpace = true;
			compositeRols = new Composite_Rols(this, SWT.NONE);
			compositeRols.setLayoutData(gridData);
			
			compositeRols.add_Composite_Rols_Listener(this);
		}
		return compositeRols;
	}


	@Override
	public void on_Composite_Options_Changed(Composite_Options_Event event) {
		// TODO Auto-generated method stub
		switch (event.getEventType()) {
		case Composite_Options_Event.Event_Types.TEAM_CAPTAIN_OK:
				set_ShowMode(getDisplay(), Composite_Options_Event.Event_Types.TEAM_CAPTAIN_OK);
			break;
		
		case Composite_Options_Event.Event_Types.TEAM_PIRATES_OK:
				set_ShowMode(getDisplay(), Composite_Options_Event.Event_Types.TEAM_PIRATES_OK);
			break;
		
		case Composite_Options_Event.Event_Types.READY_TO_PLAY_OK:
			set_ShowMode(getDisplay(), Composite_Options_Event.Event_Types.TEAM_PIRATES_OK);
		break;
		
		case Composite_Options_Event.Event_Types.TEAM_CAPTAIN_FREE:
			set_ShowMode(getDisplay(), Composite_Options_Event.Event_Types.TEAM_CAPTAIN_FREE);
		break;
		
		case Composite_Options_Event.Event_Types.TEAM_CAPTAIN_FULL:
			set_ShowMode(getDisplay(), Composite_Options_Event.Event_Types.TEAM_CAPTAIN_FULL);
		break;

		case Composite_Options_Event.Event_Types.USER_CAN_FORCE_START:
			set_ShowMode(getDisplay(), Composite_Options_Event.Event_Types.USER_CAN_FORCE_START);
		break;
		
		case Composite_Options_Event.Event_Types.USER_CANT_FORCE_START:
			set_ShowMode(getDisplay(), Composite_Options_Event.Event_Types.USER_CANT_FORCE_START);
		break;
		
		case Composite_Options_Event.Event_Types.USER_CAN_SELECT_CAPTAIN:
			set_ShowMode(getDisplay(), Composite_Options_Event.Event_Types.USER_CAN_SELECT_CAPTAIN);
		break;
		
		case Composite_Options_Event.Event_Types.USER_CAN_SELECT_BOTH:
			set_ShowMode(getDisplay(), Composite_Options_Event.Event_Types.USER_CAN_SELECT_BOTH);
		break;
		
		default:
			break;
		}
	}
	
	public void set_ShowMode(final Display disp, final int type)
	{
		Runnable runnable = new Runnable() {
		
		@Override
		public void run() {
			
			disp.syncExec(new Runnable() {
				
				@Override
				public void run() {
					if (type == Composite_Options_Event.Event_Types.TEAM_CAPTAIN_OK){
						get_Composite_Options().get_Button_Captain_Team().setEnabled(false);
						get_Composite_Options().get_Button_Pirates_Team().setEnabled(true);
						get_Composite_Rols().setVisible(true);
					}
					else if (type == Composite_Options_Event.Event_Types.TEAM_PIRATES_OK){
						get_Composite_Options().get_Button_Pirates_Team().setEnabled(false);
						get_Composite_Rols().setVisible(false);
					}
					else if (type == Composite_Options_Event.Event_Types.READY_TO_PLAY_OK){
						//getPeerList_NEW().paint_Table(woDPeers);
						//get_Composite_Options().g
					}else if (type == Composite_Options_Event.Event_Types.TEAM_CAPTAIN_FREE){
						get_Composite_Options().get_Button_Captain_Team().setEnabled(true);
					}else if (type == Composite_Options_Event.Event_Types.TEAM_CAPTAIN_FULL){
						get_Composite_Options().get_Button_Captain_Team().setEnabled(false);
					}
					else if (type == Composite_Options_Event.Event_Types.USER_CAN_FORCE_START){
						get_Composite_Options().get_Button_Start_Game().setEnabled(true);
						create_List_Maps().setVisible(true);
					}
					else if (type == Composite_Options_Event.Event_Types.USER_CANT_FORCE_START){
						get_Composite_Options().get_Button_Start_Game().setEnabled(false);
						create_List_Maps().setVisible(false);
					}
					else if (type == Composite_Options_Event.Event_Types.USER_CAN_SELECT_CAPTAIN){
						get_Composite_Options().get_Button_Captain_Team().setEnabled(true);
					}					
					else if (type == Composite_Options_Event.Event_Types.USER_CAN_SELECT_BOTH){
						get_Composite_Options().get_Button_Captain_Team().setEnabled(true);
						get_Composite_Options().get_Button_Pirates_Team().setEnabled(true);
					}
					
					//ROL EVENTS!!!
					else if (type == Composite_Rols_Event.Event_Types.SELECT_CAPTAIN_OK)
					{
						get_Composite_Rols().get_Select_Rol_Captain().setEnabled(false);
						get_Composite_Rols().get_Select_Rol_Maiden().setEnabled(true);
					}
					else if (type == Composite_Rols_Event.Event_Types.SELECT_MAIDEN_OK)
					{
						get_Composite_Rols().get_Select_Rol_Maiden().setEnabled(false);
						get_Composite_Rols().get_Select_Rol_Captain().setEnabled(true);
					}
					else if (type == Composite_Rols_Event.Event_Types.ONLY_CAN_SELECT_CAPTAIN)
					{
						get_Composite_Rols().get_Select_Rol_Maiden().setEnabled(false);
						get_Composite_Rols().get_Select_Rol_Captain().setEnabled(true);
					}
					else if (type == Composite_Rols_Event.Event_Types.ONLY_CAN_SELECT_MAIDEN)
					{
						get_Composite_Rols().get_Select_Rol_Maiden().setEnabled(true);
						get_Composite_Rols().get_Select_Rol_Captain().setEnabled(false);
					}
					else if (type == Composite_Rols_Event.Event_Types.ONLY_CAN_SELECT_BOTH)
					{
						get_Composite_Rols().get_Select_Rol_Maiden().setEnabled(true);
						get_Composite_Rols().get_Select_Rol_Captain().setEnabled(true);
					}
//					else if (type == Composite_Rols_Event.Event_Types.)
//					{
//						get_Composite_Rols().get_Select_Rol_Maiden().setEnabled(false);
//						get_Composite_Rols().get_Select_Rol_Captain().setEnabled(true);
//					}
				}
			});		
		}
	};
	
	new Thread(runnable).start(); 		
	

	}


	@Override
	public void on_Composite_Rols_Changed(Composite_Rols_Event event) {
		// TODO Auto-generated method stub
		switch (event.getEventType()) {
		case Composite_Rols_Event.Event_Types.SELECT_CAPTAIN_OK:
				set_ShowMode(getDisplay(), Composite_Rols_Event.Event_Types.SELECT_CAPTAIN_OK);
			break;
		
		case Composite_Rols_Event.Event_Types.SELECT_MAIDEN_OK:
				set_ShowMode(getDisplay(), Composite_Rols_Event.Event_Types.SELECT_MAIDEN_OK);
			break;
			
		case Composite_Rols_Event.Event_Types.ONLY_CAN_SELECT_CAPTAIN:
			set_ShowMode(getDisplay(), Composite_Rols_Event.Event_Types.ONLY_CAN_SELECT_CAPTAIN);
		break;
		
		case Composite_Rols_Event.Event_Types.ONLY_CAN_SELECT_BOTH:
			set_ShowMode(getDisplay(), Composite_Rols_Event.Event_Types.ONLY_CAN_SELECT_BOTH);
		break;
		
		
		default:
			break;
		}
	}


	@Override
	public void on_Composite_Maps_Changed(Composite_Maps_Event event) {
		// TODO Auto-generated method stub
//		switch (event.getEventType()) {
//		case Composite_Maps_Event.Event_Types.MAP_SELECTED:
//			
//			System.out.println("SELECTED MAP: " + create_List_Maps().getSelectedMap());
//			
//			break;
//		}
	}
	
}
